import { getUserInfo, setUserInfo } from '../../services/user';
Page({
  data: {
    name: '',
    idcard: '',
    userFrontImage: '',
    userBehindImage: '',
    userAllImage: ''
  },
  onShow() {
    my.setNavigationBar({
      title: '实名认证',
      backgroundColor: '#4BA3F9'
    })
    this.getUserInfo()
  },
  inputname(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  inputidcard(e) {
    this.setData({
      idcard: e.detail.value,
    });
  },
  onSubmit(e) {
    if (this.data.name.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '姓名不能为空',
        duration: 2000,
      });
      return
    } if (this.data.idcard.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '身份证不能为空',
        duration: 2000,
      });
      return
    }
    if (this.data.userFrontImage.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '身份证正面不能为空',
        duration: 2000,
      });
      return
    }
    if (this.data.userBehindImage.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '身份证背面不能为空',
        duration: 2000,
      });
      return
    }
    // if (this.data.userAllImage.length <= 0) {
    //   my.showToast({
    //     type: 'fail',
    //     content: '手持身份证自拍不能为空',
    //     duration: 2000,
    //   });
    //   return
    // }
    let param = {
      userName: this.data.name,
      userIdCard: this.data.idcard,
      userFrontImage: this.data.userFrontImage,
      userBehindImage: this.data.userBehindImage,
      // userAllImage: this.data.userAllImage,
    }
    setUserInfo(param).then(res => {
      if (res.code == "000000") {
        my.showToast({
          type: 'success',
          content: '保存成功',
          duration: 2000,
        });
        setTimeout(() => {
          my.navigateBack()
        }, 2000);
      }
    });
  },
  onReset() {
  },
  uploadFile(type) {
    my.chooseImage({
      chooseImage: 1,
      success: res => {
        const path = res.apFilePaths[0];
        my.uploadFile({
          url: 'https://alimi.xiaozubao.net/api/app/homePage/upload',
          fileType: 'image',
          fileName: 'file',
          filePath: path,
          success: res => {
            let data = JSON.parse(res.data)
            if (data.code == "000000") {
              if (type == 1) {
                this.setData({ userFrontImage: data.data })
              } else if (type == 2) {
                this.setData({ userBehindImage: data.data })
              } else if (type == 3) {
                this.setData({ userAllImage: data.data })
              }
            } else {
              my.showToast({
                type: 'fail',
                content: data.desc,
                duration: 2000,
              });
              return
            }

          },
          fail: function (res) {
            my.alert({ title: '上传失败' });
          },
        });
      },
    });
  },
  uploadFile1() {
    this.uploadFile(1)
  },
  uploadFile2() {
    this.uploadFile(2)
  },
  uploadFile3() {
    this.uploadFile(3)
  },
  getUserInfo() {
    getUserInfo({}).then(res => {
      if (res.code == "000000") {

      }
    })
  },
  regionPicker() {

    my.regionPicker({
      mergeOptions: {},
      success: (res) => {
        my.alert({
          title: 'regionPicker response: ' + JSON.stringify(res),
        })
      },
    });
  }

});
